﻿using Abp.Collections.Extensions;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Castle.Core.Logging;
using DevM.Development.ReleaseRecords;
using DevM.Development.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevM.Development.Projects
{
    public class ProjectManager : ITransientDependency, IDomainService
    {
        private readonly IRepository<Environment, long> environmentRepository;
        private readonly IRepository<Project, long> projectRepository;
        private readonly ILogger logger;
        private readonly ResourceManager resourceManager;
        private readonly ProjectStore projectStore;
        private readonly ReleaseRecordStore releaseRecordStore;

        public ProjectManager(IRepository<Environment, long> environmentRepository, IRepository<Project, long> projectRepository,ILogger logger, ResourceManager resourceManager, ProjectStore projectStore, ReleaseRecordStore releaseRecordStore)
        {
            this.environmentRepository = environmentRepository;
            this.projectRepository = projectRepository;
            this.logger = logger;
            this.resourceManager = resourceManager;
            this.projectStore = projectStore;
            this.releaseRecordStore = releaseRecordStore;
        }
        /// <summary>
        /// 项目列表
        /// </summary>
        public IQueryable<Project> Projects { get => projectRepository.GetAll(); }
        /// <summary>
        /// 项目环境
        /// </summary>
        public IQueryable<Environment> Environments { get => environmentRepository.GetAll(); }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public async Task<Project> AddProject(Project project)
        {
            project.CreationTime = DateTime.Now;
            var _project = await projectRepository.InsertAsync(project);
            await releaseRecordStore.AddReleaseRecord(_project.CreatorUserId, _project.Id, _project.TenantId);
            return _project;
        }
        /// <summary>
        /// 更新项目信息
        /// </summary>
        /// <param name="modifierUserId"></param>
        /// <param name="project"></param>
        /// <returns></returns>
        public async Task<Project> UpdateProject(Project project)
        {
            var _project = await projectRepository.GetAsync(project.Id);
            _project.LastModificationTime = DateTime.Now;
            _project.Name = project.Name;
            _project.MemberRoleId = project.MemberRoleId;
            _project.Introduction = project.Introduction;
            return await projectRepository.UpdateAsync(_project);
        }
        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task DeleteProject(long deleterUserId,params long[] projectId)
        {
            foreach (var id in projectId)
            {
                var project =await projectRepository.GetAsync(id);
                if(project!=null)
                {
                    project.DeleterUserId = deleterUserId;
                    project.DeletionTime = DateTime.Now;
                    project.IsDeleted = true;
                    await projectRepository.UpdateAsync(project);

                }
            }
        }


        /// <summary>
        /// 获取项目详情
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<Project> GetProjectById(long projectId)
        {
            if (projectId <= 0) return null;
            return await projectRepository.GetAsync(projectId);
        }
        /// <summary>
        /// 添加环境
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task<Environment> AddEnvironment(Environment environment)
        {
            environment.CreationTime = DateTime.Now;
            return await environmentRepository.InsertAsync(environment);
        }
        /// <summary>
        /// 更新项目环境
        /// </summary>
        /// <param name="modifierUserId"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task<Environment> UpdateEnvironment(Environment environment)
        {
            var _environment = await environmentRepository.GetAsync(environment.Id);
            _environment.LastModificationTime = DateTime.Now;
            _environment.Name = environment.Name;
            _environment.Host = environment.Host;
            _environment.DbConnectStr = environment.DbConnectStr;
            _environment.DbType = environment.DbType;
            _environment.Color = environment.Color;
            _environment.Host = environment.Host;
            return await environmentRepository.UpdateAsync(_environment);
        }
        /// <summary>
        /// 删除环境
        /// </summary>
        /// <param name="deleterUserId"></param>
        /// <param name="environmentId"></param>
        /// <returns></returns>
        public async Task DeleteEnvironment(long deleterUserId,params long[] environmentId)
        {
            foreach (var id in environmentId)
            {

                var env = await environmentRepository.GetAsync(id);
                env.DeleterUserId = deleterUserId;
                env.DeletionTime = DateTime.Now;
                env.IsDeleted = true;
                await environmentRepository.UpdateAsync(env);
            }
        }
        /// <summary>
        /// 根据项目获取环境
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<List<Environment>> GetEnvironmentsByProjectId(long projectId)
        {
            return await projectStore.GetEnvironmentsByProjectId(projectId);
        }
        /// <summary>
        /// 根据环境id获取环境信息
        /// </summary>
        /// <param name="environmentId"></param>
        /// <returns></returns>
        public async Task<Environment> GetEnvironmentById(long environmentId)
        {
            if (environmentId <= 0) return null;
            return await projectStore.GetEnvironmentById(environmentId);
        }

        
    }
}
